<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Project extends Auth_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('project_mdl');
    }

    public function index(){

        if($_SESSION['user']['status'] == 3){
            show_error('您没有权限打开该页面');
        }

        $url = '';
        $this->db->start_cache();
        $this->db->where('status',1);

        $sid = $this->input->get('search-sort');
        if(!empty($sid)){
            $this->db->where('sort_id',$sid);
            $url .= 'search-sort='.$sid.'&';
        }

        $key = $this->input->get('keywords');
        if(!empty($key)){
            $this->db->like('title',$key);
            $url .= 'keywords='.$key.'&';
        }
        $this->db->stop_cache();

        if(!empty($url)){
            $url = '?'.substr($url, 0 , -1);
        }

        $this->load->library('pagination');
        $config['base_url'] = site_url('project/index').$url;
        $config['total_rows'] = $this->project_mdl->get_count();
        $config['per_page'] = 10;
        $config['page_query_string'] = TRUE;
        $config['first_link'] = '首页';
        $config['last_link'] = '尾页';
        $config['next_link'] = '下一页';
        $config['prev_link'] = '上一页';
        $config['anchor_class'] = 'current';

        $this->pagination->initialize($config);
        $data['page'] = $this->pagination->create_links();

        $per_page = $this->input->get('per_page');
        $this->db->limit($config['per_page'],intval($per_page));
        $this->db->order_by('sort desc,id desc');
        $list = $this->project_mdl->select();//echo $this->db->last_query();
        $this->db->flush_cache();

        $this->load->model('sort_mdl');
        $list_sort = $this->sort_mdl->select();

        foreach($list as $k => $v){
            foreach($list_sort as $key => $value){
                if($value['id'] == $v['sort_id']){
                    $list[$k]['_sort'] = $value;
                }
            }
            if(empty($list[$k]['_sort'])){
                $list[$k]['_sort'] = '-- 未分类 --';
            }
        }
        $data['list'] = $list;
        $data['list_sort'] = $list_sort;
        $this->_view('index',$data);
    }

    /**
     * 分类项目列表
     */
    public function p_list(){
        $url = '';
        $this->db->start_cache();
        $this->db->where('status',1);

        $sid = $this->input->get('sid');
        if(!empty($sid)){
            $this->db->where('sort_id',$sid);
            $url .= 'sid='.$sid.'&';
        }else{
            $this->db->where('sort_id',0);
        }

        $key = $this->input->get('keywords');
        if(!empty($key)){
            $this->db->like('title',$key);
            $url .= 'keywords='.$key.'&';
        }
        $this->db->stop_cache();

        if(!empty($url)){
            $url = '?'.substr($url, 0 , -1);
        }

        $this->load->library('pagination');
        $config['base_url'] = site_url('project/p_list').$url;
        $config['total_rows'] = $this->project_mdl->get_count();
        $config['per_page'] = 10;
        $config['page_query_string'] = TRUE;
        $config['first_link'] = '首页';
        $config['last_link'] = '尾页';
        $config['next_link'] = '下一页';
        $config['prev_link'] = '上一页';
        $config['anchor_class'] = 'current';

        $this->pagination->initialize($config);
        $data['page'] = $this->pagination->create_links();

        $per_page = $this->input->get('per_page');
        $this->db->limit($config['per_page'],intval($per_page));
        $this->db->order_by('sort desc,id desc');
        $list = $this->project_mdl->select();//echo $this->db->last_query();
        $this->db->flush_cache();

        $this->load->model('sort_mdl');
        $this->load->model('order_mdl');
        $this->load->model('parameters_mdl','par_mdl');
        $list_sort = $this->sort_mdl->select();

        foreach($list_sort as $key => $value){
            if($value['id'] == $sid){
                $data['sort'] = $value['title'];
            }
        }
        if(empty($data['sort'])){
            $data['sort'] = '其他分类';
        }

        foreach($list as $k => $v){
            //查询属性
            $this->db->where('project_id', $v['id']);
            $this->db->select('id,title,update_time');
            $list[$k]['par'] = $this->par_mdl->select();
            //查询销售数量
            $list[$k]['count'] = 0;
            foreach($list[$k]['par'] as $key => $value){
                $this->db->where('status !=',6);
                $this->db->where('parameters_id',$value['id']);
                $list[$k]['count'] += $this->order_mdl->get_count();
            }
        }
        $data['list'] = $list;
        $data['list_sort'] = $list_sort;
        $this->_view('list',$data);
    }

    public function show(){
        $pro_id = $this->input->get('pid');
        $pro_id = intval($pro_id);

        $this->db->where('id',$pro_id);
        $this->db->where('status',1);
        $res = $this->project_mdl->get_one();

        if(empty($res)){
            show_error('该项目不存在，或者已经被下架。');
        }

        //查询文章
        $this->load->model('article_mdl');
        $this->db->where('id',$res['article_id']);
        $article = $this->article_mdl->get_one();

        //查询项目属性
        $this->load->model('parameters_mdl');
        $this->db->where('project_id',$res['id']);
        $this->db->where('status',1);
        $this->db->select('id,title');
        $pro_list = $this->parameters_mdl->select();

        $this->_view('show',array('res'=>$res,'article'=>$article,'list'=>$pro_list));
    }

    /**
     * 项目删除
     */
    public function delete(){
        if(!$this->input->is_ajax_request()){
            show_error('参数错误！');
        }
        if($_SESSION['user']['status'] == 3){
            show_error('您没有权限打开该页面');
        }

        $pid = $this->input->post('pid');
        $pid = intval($pid);

        $this->db->where('id',$pid);
        $res = $this->project_mdl->update(array('status'=>3));

        echo $res === true ? 1 : '操作失败！';
    }

    public function create(){
        if($_SESSION['user']['status'] == 3){
            show_error('您没有权限打开该页面');
        }

        if($this->input->is_ajax_request()){
            $post = $this->input->post();

            if(empty($post['title'])){
                die('项目名称不能为空');
            }
            if(intval($post['article_id']) <= 0){
                die('请选择文档');
            }

            $data = array(
                'title' => $post['title'],
                'article_id' => intval($post['article_id']),
                'create_time' => time(),
                'status' => 1,
                'sort' => 0,
                'sort_id' => intval($post['sort_id'])
            );

            $res = $this->project_mdl->create($data);

            echo $res;
        }else{

            $this->load->model('sort_mdl');
            $data['list_sort'] = $this->sort_mdl->select();
            $this->_view('create',$data);
        }
    }

    /**
     * 修改权限
     */
    public function update(){
        if($_SESSION['user']['status'] == 3){
            show_error('您没有权限打开该页面');
        }

        if($this->input->is_ajax_request()){
            $post = $this->input->post();
            if(empty($post['title'])){
                die('项目名称不能为空');
            }
            if(intval($post['article_id']) <= 0){
                die('请选择文档');
            }

            $data = array(
                'title' => $post['title'],
                'article_id' => intval($post['article_id']),
                'create_time' => time(),
                'sort_id' => intval($post['sort_id'])
            );

            $this->db->where('id',intval($post['pid']));
            $res = $this->project_mdl->update($data);

            echo $res === true ? '1' : '操作失败！';
        }else{
            $pid = $this->input->get('pid');

            $this->db->where('id',intval($pid));
            $res = $this->project_mdl->get_one();

            if(empty($res)){
                show_error('该项目不存在');
            }

            $this->load->model('article_mdl');
            $this->db->where('id',$res['article_id']);
            $res_art = $this->article_mdl->get_one();
            $res['article_title'] = empty($res_art['title']) ? '' : $res_art['title'];

            $this->load->model('sort_mdl');
            $list_sort = $this->sort_mdl->select();
            $this->_view('update',array('res'=>$res,'list_sort'=>$list_sort));
        }
    }

    /**
     * 设置排序
     */
    public function set_sort(){
        if($_SESSION['user']['status'] == 3){
            show_error('您没有权限打开该页面');
        }

        if(!$this->input->is_ajax_request()){
            show_error('参数错误!');
        }

        $post = $this->input->post();
        $sort = intval($post['sort']);
        if($sort < 0){
            die('排序数值需为正整数');
        }
        $post = $this->input->post();

        $this->db->where('id',intval($post['pid']));

        $res = $this->project_mdl->update(array('sort'=>$sort,'create_time'=>time()));

        echo $res === true ? 1 : '操作失败！';
    }

}

/* End of file Project.php */
/* Location: ./application/controllers/Project.php */